package vn.edu.ute.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import vn.edu.ute.pojo.DoanVien;

@Repository
public class DoanVienDAOImp implements DoanVienDAO {
	@Autowired
	private SessionFactory sessionFactory;

	public boolean themHoacCapNhatDoanVien(DoanVien dv) {
		Session session = sessionFactory.openSession();
		Transaction trans = session.beginTransaction();
		try {
			session.saveOrUpdate(dv);
			trans.commit();
		} catch (Exception e) {
			trans.rollback();
		} finally {
			session.close();
		}
		return true;
	}

	public boolean xoaDoanVien(DoanVien dv) {
		Session session = sessionFactory.openSession();
		Transaction trans = session.beginTransaction();
		try {
			session.delete(dv);
			trans.commit();
		} catch (Exception e) {
			trans.rollback();
		} finally {
			session.close();
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	public List<DoanVien> layDSDoanVienTheoChiDoan(String maChiDoan) {
		List<DoanVien> dsDoanVien=null;
		Session session = sessionFactory.openSession();
    	String hsql = "from DoanVien where chiDoan.maChiDoan = :id";
    	Query query = session.createQuery(hsql);
    	query.setString("id",maChiDoan);
    	dsDoanVien = query.list();
    	session.close();
    	return dsDoanVien;
	}

	public List<DoanVien> layDSDoanVienTheoChiDoanGioiHan(String maChiDoan,
			int vtBatDau, int soDong) {
		// TODO Auto-generated method stub
		return null;
	}

}
